package com.whq.knowverywell.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.whq.knowverywell.R;
import com.whq.knowverywell.activity.ContentFragment.OnSumDataListener;
import com.whq.knowverywell.adapter.ContentFragmentAdapter;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ParamUtil;
import com.whq.knowverywell.view.PageIndicator;
import com.whq.knowverywell.view.TitlePageIndicator;

/**
 * content frame
 * 
 * @author daiwei
 *
 */
public class ContentFrameActivity extends BaseLoginActivity implements ActionBar.OnNavigationListener, OnSumDataListener{
	private static final String TAG = ContentFrameActivity.class.getSimpleName();
	
	private ViewPager mViewPager;
	private PageIndicator mPageIndicator;
	private ContentFragmentAdapter mFragmentAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreate start ...");
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content_frame);
		
		// init content fragment 
		mFragmentAdapter = new ContentFragmentAdapter(getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mFragmentAdapter);
		mPageIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
		mPageIndicator.setViewPager(mViewPager);
		mSumTextView = (TextView) findViewById(R.id.sum_textview);
		mAmountTextView = (TextView) findViewById(R.id.amount_textview);
		mPageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		// set default page today
		mPageIndicator.setCurrentItem(1);
		
	}
	
	@Override
	protected void getParameters() {
		super.getParameters();
		Intent intent = getIntent();
		if (intent != null) {
			itemId = intent.getIntExtra(ParamUtil.BUNDLE_KEY_ITEM_ID, -1);
			LogUtil.i(TAG, "parameter item id is : " + itemId);
		}
	}
	
	@Override
	protected void onResume() {
		LogUtil.d(TAG, "onResume start ...");
		super.onResume();
		
	}
	
	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		Toast.makeText(this, "Selected: " + itemTitles[itemPosition], Toast.LENGTH_SHORT).show();
		this.itemId = itemPosition + 1;
		
		// change data 
		mFragmentAdapter = new ContentFragmentAdapter(getSupportFragmentManager());
		mFragmentAdapter.setBusinessType(this.itemId);
	    mViewPager.setAdapter(mFragmentAdapter);
	    mFragmentAdapter.notifyDataSetChanged();
		
		return true;
	}

	@Override
	public void onSumData(long totalSalesVolume, double totalSalesAmount) {
//		mSumTextView.setText(totalSalesVolume + "");
//		mAmountTextView.setText("￥" + totalSalesAmount + "");
	}

}
